{#**
 * 2007-2017 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2017 PrestaShop SA
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * International Registered Trademark & Property of PrestaShop SA
 *#}
{% form_theme categories _self %}
{% extends 'PrestaShopBundle:Admin:layout.html.twig' %}

{% block javascripts %}
  {{ parent() }}
  <script src="{{ asset('themes/default/js/bundle/product/catalog.js') }}"></script>
  <script src="{{ asset('themes/default/js/bundle/pagination.js') }}"></script>
  <script src="{{ asset('themes/default/js/bundle/category-tree.js') }}"></script>
  <script src="{{ asset('../js/jquery/ui/jquery.ui.sortable.min.js') }}"></script>
{% endblock %}

{% block choice_tree_widget -%}
    <div {{ block('widget_container_attributes') }}>
        <ul class="category-tree">
        {%- for child in choices %}
            {{ block('choice_tree_item_widget') }}
        {% endfor -%}
        </ul>
    </div>
{%- endblock choice_tree_widget %}

{% block choice_tree_item_widget -%}
    <li>
        {% set checked = (form.vars.submitted_values is defined and submitted_values[child.id_category] is defined) ? 'checked="checked"' : '' %}

         <div class="radio">
             <label class="category-label" for="form[{{ form.vars.id }}][tree]">{{ child.name }}
                 <input
                   type="radio"
                   name="form[{{ form.vars.id }}][tree]"
                   value="{{ child.id_category }}" {{ checked }}
                   class="category pull-right"
                 />
             </label>
         </div>

        {% if child.children is defined %}
            <ul>
                {% for item in child.children %}
                    {% set child = item %}
                    {{ block('choice_tree_item_widget') }}
                {% endfor -%}
            </ul>
        {% endif %}
    </li>
{%- endblock choice_tree_item_widget %}

{% block content %}

  <div class="products-catalog">

    {{ renderhook('legacy_block_kpi', {'kpi_controller': 'AdminProductsController'}) }}

    <div class="content container-fluid">

      {% if permission_error|length %}
      <div class="row">
        <div class="col-md-12">
          <div class="alert alert-danger">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <i class="material-icons">error_outline</i>
            <p>
              {{ permission_error }}
            </p>
          </div>
        </div>
      </div>
      {% endif %}

      <div class="row">
        <div class="col-md-12">
          <div class="pull-right">
            <a id="desc-product-export" class="list-toolbar-btn" href="{{ path('admin_product_export_action') }}">
              {{ ps.tooltip(("Export"|trans({}, 'Admin.Actions')), 'cloud_upload') }}
            </a>
            <a id="desc-product-import" class="list-toolbar-btn" href="{{ import_link }}">
              {{ ps.tooltip(("Import"|trans({}, 'Admin.Actions')), 'cloud_download') }}
            </a>
            <a id="desc-product-show-sql" class="list-toolbar-btn" href="javascript:void(0);" onclick="showLastSqlQuery();">
              {{ ps.tooltip(("Show SQL query"|trans({}, 'Admin.Actions')), 'code') }}
            </a>
            <a id="desc-product-sql-manager" class="list-toolbar-btn" href="javascript:void(0);" onclick="sendLastSqlQuery(createSqlQueryName());">
              {{ ps.tooltip(("Export to SQL Manager"|trans({}, 'Admin.Actions')), 'storage') }}
            </a>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-md-1">
          <div class="checkbox">
            <label>
              <input
                type="checkbox"
                id="bulk_action_select_all"
                onclick="$('#product_catalog_list').find('table td.checkbox-column input:checkbox').prop('checked', $(this).prop('checked')); updateBulkMenu();"
              />
              {{ "Select all"|trans({}, 'Admin.Actions') }}
            </label>
          </div>
        </div>

        <div
          class="col-md-2"
          bulkurl="{{ path('admin_product_bulk_action', {'action': 'activate_all'}) }}"
          massediturl="{{ path('admin_product_mass_edit_action', {'action': 'sort'}) }}"
          redirecturl="{{ path('admin_product_catalog', {'limit': limit, 'offset': offset, 'orderBy': orderBy, 'sortOrder': sortOrder}) }}"
          redirecturlnextpage="{{ path('admin_product_catalog', {'limit': limit, 'offset': offset+limit, 'orderBy': orderBy, 'sortOrder': sortOrder}) }}"
        >
          {% set buttons_action = [
          {
          "onclick": "bulkProductAction(this, 'activate_all');",
          "icon": "radio_button_checked",
          "label": "Activate selection"|trans({}, 'Admin.Actions')
          },
          {
          "onclick": "bulkProductAction(this, 'deactivate_all');",
          "icon": "radio_button_unchecked",
          "label": "Deactivate selection"|trans({}, 'Admin.Actions')
          }
          ] %}

          {# Only duplicate if shop or group is selected #}
          {% if is_shop_context %}
            {% set buttons_action = buttons_action|merge([
            {
            "divider": true
            },
            {
            "onclick": "bulkProductAction(this, 'duplicate_all');",
            "icon": "content_copy",
            "label": "Duplicate selection"|trans({}, 'Admin.Actions')
            }
            ]) %}
          {% endif %}

          {% set buttons_action = buttons_action|merge([
          {
          "divider": true
          },
          {
          "onclick": "bulkProductAction(this, 'delete_all');",
          "icon": "delete",
          "label": "Delete selection"|trans({}, 'Admin.Actions')
          }
          ]) %}

          {% include 'PrestaShopBundle:Admin/Helpers:dropdown_menu.html.twig' with {
            'div_style': "btn-group dropup",
            'button_id': "product_bulk_menu",
            'disabled': true,
            'menu_label': "Bulk actions"|trans({}, 'Admin.Global'),
            'buttonType': "tertiary-outline",
            'menu_icon': "icon-caret-up",
            'items': buttons_action
          }%}
        </div>
        <div id="product_catalog_category_tree_filter" class="pull-right col-md-3">
          <button
            class="btn btn-tertiary-outline"
            data-toggle="collapse"
            data-target="#tree-categories"
          >
          {{ 'Filter by categories'|trans({}, 'Admin.Actions') }}
          <i class="material-icons">expand_more</i>
          </button>
          <div id="tree-categories" class="collapse p-t-1">
            <a
              class="categories-tree-actions"
              href="#"
              name="product_catalog_category_tree_filter_expand"
              onclick="productCategoryFilterExpand($('div#product_catalog_category_tree_filter'), this);"
              id="product_catalog_category_tree_filter_expand"
            >
              <i class="material-icons">expand_more</i>
              {{ 'Expand'|trans({}, 'Admin.Actions') }}
            </a>
            <a
            class="categories-tree-actions"
              href="#"
              name="product_catalog_category_tree_filter_collapse"
              onclick="productCategoryFilterCollapse($('div#product_catalog_category_tree_filter'), this);"
              id="product_catalog_category_tree_filter_collapse"
            >
              <i class="material-icons">expand_less</i>
              {{ 'Collapse'|trans({}, 'Admin.Actions') }}
            </a>
            <a
              class="categories-tree-actions"
              href="#"
              name="product_catalog_category_tree_filter_reset"
              onclick="productCategoryFilterReset($('div#product_catalog_category_tree_filter'));"
              id="product_catalog_category_tree_filter_reset"
            >
              <i class="material-icons">radio_button_unchecked</i>
              {{ 'Unselect'|trans({}, 'Admin.Actions') }}
            </a>
            <hr>
            {{ form_widget(categories) }}
          </div>
        </div>
      </div>

      <form
        name="product_catalog_list"
        id="product_catalog_list"
        method="post"
        action="{{ path('admin_product_catalog', {'limit': limit, 'orderBy': orderBy, 'sortOrder': sortOrder}) }}"
        orderingurl="{{ path('admin_product_catalog', {'limit': limit, 'orderBy': 'name', 'sortOrder': 'asc'}) }}"
        newproducturl="{{ path('admin_product_new') }}"
      >
        <div class="row">
          <div class="col-md-12">
            <input type="hidden" name="filter_category" value="{{ filter_category|default('') }}" />
          </div>
        </div>

        <div class="row">
          <div class="col-md-12">
            <table
              class="table table-condensed table-striped product m-t-1"
              redirecturl="{{ path('admin_product_catalog', {
                'limit': limit,
                'offset': offset,
                'orderBy': orderBy,
                'sortOrder': sortOrder})
              }}"
            >
              <thead>
                <tr class="column-headers">
                  <th style="width: 8%">
                    {{ "ID"|trans({}, 'Admin.Global') }}
                    {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                      'column': 'id_product'
                    } %}
                  </th>
                  <th>
                    {{ "Image"|trans({}, 'Admin.Global') }}
                  </th>
                  <th>
                    {{ "Name"|trans({}, 'Admin.Global') }}
                    {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                      'column': 'name'
                    } %}
                  </th>
                  <th style="width: 9%">
                    {{ "Reference"|trans({}, 'Admin.Global') }}
                    {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                      'column': 'reference'
                    } %}
                  </th>
                  <th>
                    {{ "Category"|trans({}, 'Admin.Catalog.Feature') }}
                    {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                      'column': 'name_category'
                    } %}
                  </th>
                  <th style="width: 9%">
                    {{ "Price (tax excl.)"|trans({}, 'Admin.Catalog.Feature') }}
                    {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                      'column': 'price'
                    } %}
                  </th>

                  {% if 'PS_STOCK_MANAGEMENT'|configuration %}
                  <th style="width: 9%">
                    {{ "Quantity"|trans({}, 'Admin.Catalog.Feature') }}
                    {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                      'column': 'sav_quantity'
                    } %}
                  </th>
                  {% else %}
                    <th></th>
                  {% endif %}

                  <th>
                    {{ "Status"|trans({}, 'Admin.Global') }}
                    {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                      'column': 'active'
                    } %}
                  </th>
                  {% if has_category_filter == true %}
                    <th>
                      {{ "Position"|trans({}, 'Admin.Global') }}
                      {% include 'PrestaShopBundle:Admin/Product/Include:catalog_order_carrets.html.twig' with {
                        'column': 'position'
                      } %}
                    </th>
                  {% endif %}
                  <th style="width: 6%"></th>
                </tr>
                <tr class="column-filters">
                  <th>
                    {% include 'PrestaShopBundle:Admin/Helpers:range_inputs.html.twig' with {
                      'input_name': "filter_column_id_product",
                      'min': '0',
                      'max': '1000000',
                      'minLabel': "Min"|trans({}, 'Admin.Global'),
                      'maxLabel': "Max"|trans({}, 'Admin.Global'),
                      'value': filter_column_id_product,
                    } %}
                  </th>
                  <th>&nbsp;</th>
                  <th>
                    <input
                      type="text"
                      class="form-control"
                      placeholder="{{ "Search name"|trans({}, 'Admin.Catalog.Help') }}"
                      name="filter_column_name"
                      value="{{ filter_column_name }}"
                    />
                  </th>
                  <th>
                    <input
                      type="text"
                      class="form-control"
                      placeholder="{{ "Search ref."|trans({}, 'Admin.Catalog.Help') }}"
                      name="filter_column_reference"
                      value="{{ filter_column_reference }}"
                    />
                  </th>
                  <th>
                    <input
                      type="text"
                      class="form-control"
                      placeholder="{{ "Search category"|trans({}, 'Admin.Catalog.Help') }}"
                      name="filter_column_name_category"
                      value="{{ filter_column_name_category }}"
                    />
                  </th>
                  <th>
                    {% include 'PrestaShopBundle:Admin/Helpers:range_inputs.html.twig' with {
                      'input_name': "filter_column_price",
                      'min': '0',
                      'max': '1000000',
                      'minLabel': "Min"|trans({}, 'Admin.Global'),
                      'maxLabel': "Max"|trans({}, 'Admin.Global'),
                      'value': filter_column_price,
                    } %}
                  </th>

                  {% if 'PS_STOCK_MANAGEMENT'|configuration %}
                  <th>
                    {% include 'PrestaShopBundle:Admin/Helpers:range_inputs.html.twig' with {
                      'input_name': "filter_column_sav_quantity",
                      'min': '-1000000',
                      'max': '1000000',
                      'minLabel': "Min"|trans({}, 'Admin.Global'),
                      'maxLabel': "Max"|trans({}, 'Admin.Global'),
                      'value': filter_column_sav_quantity,
                    } %}
                  </th>
                  {% else %}
                    <th></th>
                  {% endif %}

                  <th>
                    <select data-toggle="select2" name="filter_column_active">
                      <option value=""></option>
                      <option value="1" {% if (filter_column_active is defined) and filter_column_active == '1' %}selected="selected"{% endif %}>Active</option>
                      <option value="0" {% if (filter_column_active is defined) and filter_column_active == '0' %}selected="selected"{% endif %}>Inactive</option>
                    </select>
                  </th>
                  {% if has_category_filter == true %}
                    <th>
                      {% if not(activate_drag_and_drop) %}
                        <input type="button" class="btn btn-tertiary-outline" name="products_filter_position_asc" value="{{ "Reorder"|trans({}, 'Admin.Actions') }}" onclick="productOrderPrioritiesTable();" />
                        {% else %}
                        <input type="button" id="bulk_edition_save_keep" class="btn" onclick="bulkProductAction(this, 'edition');" value="{{ "Save & refresh"|trans({}, 'Admin.Actions')|raw }}" />
                    {% endif %}

                    </th>
                  {% endif %}
                  <th style="width: 12%">
                    <button
                      type="submit"
                      class="btn btn-primary"
                      name="products_filter_submit"
                      title="{{ "Filter"|trans({}, 'Admin.Actions') }}"
                    >
                      <i class="material-icons">search</i>
                      {{ "Apply"|trans({}, 'Admin.Actions') }}
                    </button>
                    <button
                      type="reset"
                      class="btn btn-invisible"
                      name="products_filter_reset"
                      onclick="productColumnFilterReset($(this).closest('tr.column-filters'))"
                      title="{{ "Reset"|trans({}, 'Admin.Actions') }}"
                    >
                      <i class="material-icons">clear</i>
                      {{ "Reset"|trans({}, 'Admin.Actions') }}
                    </button>
                  </th>
                </tr>
              </thead>
              {{ render(controller('PrestaShopBundle\\Controller\\Admin\\ProductController::listAction', {
                'limit': limit,
                'offset': offset,
                'orderBy': orderBy,
                'sortOrder': sortOrder,
                'products': products,
                'last_sql': last_sql
              })) }}
            </table>
          </div>
        </div>

        {% if product_count_filtered > 20 %}
          <div class="row">
            <div class="col-md-12">
              {{ render(controller('PrestaShopBundle:Admin\\Common:pagination',
              {'limit': limit, 'offset': offset, 'total': product_count_filtered, 'caller_parameters': pagination_parameters, 'limit_choices': pagination_limit_choices}))
              }}
            </div>
          </div>
        {% endif %}

      </form>

    </div>
  </div>

  {# Duplication product modal #}
  {% embed 'PrestaShopBundle:Admin/Helpers:bootstrap_popup.html.twig' with {
    'id': "catalog_duplicate_all_modal",
    'title': "Duplicating products"|trans({}, 'Admin.Catalog.Notification'),
    'closable': true,
    'progressbar': {
    'id': "catalog_duplicate_all_progression",
    'label': "Duplicating..."|trans({}, 'Admin.Catalog.Notification')
  },
    'actions': [],
  }%}
    {% block content %}
      <div class="modal-body">
        {{ 'Duplication in progress...'|trans({}, 'Admin.Catalog.Notification') }}
        <span id="catalog_duplicate_all_failure" style="display: none;color: darkred;">
          {{ 'Duplication failed.'|trans({}, 'Admin.Catalog.Notification') }}
        </span>
      </div>
    {% endblock %}
  {% endembed %}


  {# Activation product modal #}
  {% embed 'PrestaShopBundle:Admin/Helpers:bootstrap_popup.html.twig' with {
  'id': "catalog_activate_all_modal",
  'title': "Activating products"|trans({}, 'Admin.Catalog.Notification'),
  'closable': true,
  'progressbar': {
  'id': "catalog_activate_all_progression",
  'label': "Activating..."|trans({}, 'Admin.Catalog.Notification')
  },
  'actions': [],
  }%}
    {% block content %}
      <div class="modal-body">
        {{ 'Activation in progress...'|trans({}, 'Admin.Catalog.Notification') }}
        <span id="catalog_activate_all_failure" style="display: none;color: darkred;">
          {{ 'Activation failed.'|trans({}, 'Admin.Catalog.Notification') }}
        </span>
      </div>
    {% endblock %}
  {% endembed %}


  {# Desactivation product modal #}
  {% embed 'PrestaShopBundle:Admin/Helpers:bootstrap_popup.html.twig' with {
  'id': "catalog_deactivate_all_modal",
  'title': "Deactivating products"|trans({}, 'Admin.Catalog.Notification'),
  'closable': true,
  'progressbar': {
  'id': "catalog_deactivate_all_progression",
  'label': "Deactivating..."|trans({}, 'Admin.Catalog.Notification')
  },
  'actions': [],
  }%}
    {% block content %}
      <div class="modal-body">
        {{ 'Deactivation in progress...'|trans({}, 'Admin.Catalog.Notification') }}
        <span id="catalog_deactivate_all_failure" style="display: none;color: darkred;">
          {{ 'Deactivation failed.'|trans({}, 'Admin.Catalog.Notification') }}
        </span>
      </div>
    {% endblock %}
  {% endembed %}


  {# Deletion product modal #}
  {% embed 'PrestaShopBundle:Admin/Helpers:bootstrap_popup.html.twig' with {
  'id': "catalog_delete_all_modal",
  'title': "Deleting products"|trans({}, 'Admin.Catalog.Notification'),
  'closable': true,
  'progressbar': {
  'id': "catalog_delete_all_progression",
  'label': "Deleting..."|trans({}, 'Admin.Catalog.Notification')
  },
  'actions': [],
  }%}
    {% block content %}
      <div class="modal-body">
        {{ 'Deletion in progress...'|trans({}, 'Admin.Catalog.Notification') }}
        <span id="catalog_delete_all_failure" style="display: none;color: darkred;">
          {{ 'Deletion failed.'|trans({}, 'Admin.Catalog.Notification') }}
        </span>
      </div>
    {% endblock %}
  {% endembed %}


  {# Confirmation deletion product modal #}
  {% embed 'PrestaShopBundle:Admin/Helpers:bootstrap_popup.html.twig' with {
    'id': "catalog_deletion_modal",
    'title': "Delete products?"|trans({}, 'Admin.Catalog.Feature'),
    'closable': true,
    'actions': [{
      'type': 'button',
      'label': "Delete now"|trans({}, 'Admin.Actions'),
      'value': 'confirm',
      'class': 'btn btn-primary btn-lg',
    }],
  }%}
    {% block content %}
      <div class="modal-body">
        {{ 'These products will be deleted for good. Please confirm.'|trans({}, 'Admin.Catalog.Feature') }}
      </div>
    {% endblock %}
  {% endembed %}

  {% embed 'PrestaShopBundle:Admin/Helpers:bootstrap_popup.html.twig' with {
    'id': "catalog_sql_query_modal",
    'title': "SQL query"|trans({}, 'Admin.Global'),
    'closable': true,
    'actions': [{
      'type': 'button',
      'label': "Export to SQL Manager"|trans({}, 'Admin.Actions'),
      'value': 'sql_manager',
      'class': 'btn btn-primary btn-lg',
    }],
  } %}
    {% block content %}
      <form method="post" action="{{ sql_manager_add_link }}" id="catalog_sql_query_modal_content">
        <div class="modal-body">
          <textarea name="sql" rows="20" cols="65"></textarea>
          <input type="hidden" name="name" value="" />
        </div>
      </form>
    {% endblock %}
  {% endembed %}

{% endblock %}
